﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class XemPhieuThanhToan : Form
    {
        private string _maPhieu;
        _DB.PhieuThanhToan p;

        public XemPhieuThanhToan(string maPhieuThanhToan)
        {
            InitializeComponent();
            _maPhieu = maPhieuThanhToan;
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            p = dc.PhieuThanhToans.Single(x => x.MaPhieu == _maPhieu);
            _DB.QuyTrinhMuaHang qt = p.QuyTrinhMuaHangs.Single();

            // fill info
            txtMaPhieu.Text = p.MaPhieu;
            txtNguoiTao.Text = p.NguoiTao.Ten;
            txtPhongBan.Text = (p.NguoiTao.PhongBan == null) ? "" : p.NguoiTao.PhongBan.TenPhongBan;
            txtNgayTao.Text = p.NgayTao.ToLongTimeString();
            txtNoiDung.Text = p.NoiDung;

            // fill tien
            txtTongTien.Text = qt.PhieuMuaHang.getTongTien().ToString("#,#.##");
            if (qt.PhieuTamUng != null)
                txtTienTamUng.Text = qt.PhieuTamUng.SoTien.ToString("#,#.##");
            float tienThanhToan = qt.PhieuMuaHang.getTongTien();
            if (qt.PhieuTamUng != null)
                tienThanhToan -= qt.PhieuTamUng.SoTien;
            txtTienThanhToan.Text = (tienThanhToan > 0) ? "Phải trả  " + tienThanhToan.ToString("#,#.##") : "Phải thu  " + (-tienThanhToan).ToString("#,#.##");

            // phieu lien quan
            txtPhieuMuaHang.Text = qt.MaPhieuMuaHang;
            if (qt.PhieuTamUng != null)
                txtPhieuTamUng.Text = qt.MaPhieuTamUng;
            else
                btnXemPhieuTU.Enabled = false;
        }
        private void XemPhieuThanhToan_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            OtherForms.XemPhieuMuaHang f = new OtherForms.XemPhieuMuaHang(txtPhieuMuaHang.Text);
            f.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OtherForms.XemPhieuTamUng f = new OtherForms.XemPhieuTamUng(txtPhieuTamUng.Text);
            f.Show();
        }

        private void btnQuyTrinh_Click(object sender, EventArgs e)
        {
            XemQuyTrinh f = new XemQuyTrinh(p.QuyTrinhMuaHangs.Single().ID);
            f.Show();
        }

    }
}
